package com.example.springsecuritydemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author 朱学鑫
 * @projectName springSecurity-Demo
 * @description: Security配置类 第二种方式
 * @date 2022年02月22日 15:53
 */
//@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 重写configure方法
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 密码解析器 springSecurity自带的密码解析器,也可以自定义密码解析器
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        // 加密
        String password = passwordEncoder.encode("nbPlus");
        System.out.println("加密之后的密码:"+password);
        //passwordEncoder.matches()
        //
        auth.inMemoryAuthentication().withUser("zhuxuexin").password(password).roles("admin");
    }

    /**
     * 表示把参数按照特定的解析规则进行解析
     * @return
     */
    @Bean
    PasswordEncoder password() {
        return new BCryptPasswordEncoder();
    }
}
